
package com.borqs.music.tasks;

import static com.borqs.music.http.RequestMethod.GET;
import static com.borqs.music.util.MusicConf.ReqParam.COUNT;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskListener;
import com.borqs.music.http.HttpRequest;
import com.borqs.music.http.HttpResponse;
import com.borqs.music.util.MusicConf;
import com.borqs.music.util.MusicConf.ReqType;

public class GetHotSearchTask extends Task {
    private static final String DEFAULT_COUNT = "6";
    private String mTicket;

    public GetHotSearchTask(TaskListener searchListener, String ticket) {
        super(searchListener);
        mTicket = ticket;
    }

    @Override
    public HttpRequest prepareRequest() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(COUNT, DEFAULT_COUNT);

        String url = MusicConf.getUrl(ReqType.MUSIC_HOT_SEARCH);
        HttpRequest req = new HttpRequest(GET, url, mTicket, params);
        return req;
    }

    @Override
    public Object handleResponse(HttpResponse resp) throws Exception {
        checkResponse(resp);
        JSONArray jArr = resp.asJSONArray();
        List<String> hots = new ArrayList<String>();

        for (int i = 0, len = jArr.length(); i < len; i++) {
            JSONObject jObj = jArr.getJSONObject(i);
            String keyword = jObj.getString("keyword");
            hots.add(keyword);
        }
        return hots;
    }
}
